package com.wy.allthingdone.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by 38270 on 2016/11/21.
 */

public class ECGView extends View {
    private Paint mPaint;// 画笔
    private Path mPath;// 路径对象

    private int screenW, screenH;// 屏幕宽高
    private float x, y;// 路径初始坐标
    private float initScreenW;// 屏幕初始宽度
    private float initX;// 初始X轴坐标
    private float transX, moveX;// 画布移动的距离

    private boolean isCanvasMove;// 画布是否需要平移
    private Paint mPaint1;
    private int mheight;
    private int mwidth;

    public ECGView(Context context, AttributeSet set) {
        super(context, set);

		/*
         * 实例化画笔并设置属性
		 */
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(2);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setShadowLayer(7, 0, 0, Color.YELLOW);//设置线边色
        init();
        mPath = new Path();
        transX = 0;
        isCanvasMove = false;
    }

    @Override
    public void onSizeChanged(int w, int h, int oldw, int oldh) {
        /*
         * 获取屏幕宽高
		 */
        screenW = w;
        screenH = h;

		/*
         * 设置起点坐标
		 */
        x = 0;
        //+ (screenH / 4) + (screenH / 10)
        y = (screenH / 2);

        // 屏幕初始宽度
        initScreenW = screenW;

        // 初始X轴坐标
        initX = ((screenW / 2) + (screenW / 4));

        moveX = (screenW / 24);

        mPath.moveTo(x + getPaddingLeft(), y);//以x,y为起点 开始画线
        x = x + getPaddingLeft();
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        drawXYhistonal(canvas);
        mPath.lineTo(x, y);

        // 向左平移画布
        canvas.translate(-transX, 0);

        // 计算坐标
        calCoors();

        // 绘制路径
        canvas.drawPath(mPath, mPaint);
        invalidate();
    }

    private void init() {//
        mPaint1 = new Paint();
        mPaint1.setAntiAlias(true);
        mPaint1.setColor(Color.BLACK);
        mPaint1.setStrokeWidth(3);
        mPaint1.setStyle(Paint.Style.FILL);
    }

    private void drawXYhistonal(Canvas canvas) {
        canvas.drawLine(getPaddingLeft(), mheight - getPaddingBottom(),
                mwidth - getPaddingRight(), mheight - getPaddingBottom(),
                mPaint1);//两点之间的线
        canvas.drawCircle(getPaddingLeft(), mheight - getPaddingBottom(), 5, mPaint1);//画圆点
        canvas.drawLine(mwidth - getPaddingRight(), mheight - getPaddingBottom(), mwidth - getPaddingRight() - 20, mheight - getPaddingBottom() - 10, mPaint1);
        canvas.drawLine(mwidth - getPaddingRight(), mheight - getPaddingBottom(), mwidth - getPaddingRight() - 20, mheight - getPaddingBottom() + 10, mPaint1);
        canvas.drawLine(getPaddingLeft(), mheight - getPaddingBottom(), getPaddingLeft(), getPaddingBottom(), mPaint1);
        canvas.drawLine(getPaddingLeft(), getPaddingBottom(), getPaddingRight() - 10, getPaddingBottom() + 20, mPaint1);
        canvas.drawLine(getPaddingLeft(), getPaddingBottom(), getPaddingRight() + 10, getPaddingBottom() + 20, mPaint1);
    }

    /**
     * 计算坐标
     */
    private void calCoors() {
        if (isCanvasMove == true) {
            transX += 4;
        }
        if (x < initX) {
            x += 8;
        } else {
            if (x < initX + moveX) {
                x += 2;
                y -= 8;
            } else {
                if (x < initX + (moveX * 2)) {
                    x += 2;
                    y += 14;
                } else {
                    if (x < initX + (moveX * 3)) {
                        x += 2;
                        y -= 12;
                    } else {
                        if (x < initX + (moveX * 4)) {
                            x += 2;
                            y += 6;
                        } else {
                            if (x < initScreenW) {
                                x += 8;
                            } else {
                                isCanvasMove = true;
                                initX = initX + initScreenW;
                            }
                        }
                    }
                }
            }

        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.d("MyView", "---widthSpecSize = " + widthSpecSize + "");
        Log.d("MyView", "---heightSpecSize = " + heightSpecSize + "");
        if (widthSpecSize == MeasureSpec.AT_MOST) {//wrapcontent
            Log.d("MyView", "---AT_MOST---");
        }
        if (widthSpecSize == MeasureSpec.EXACTLY) {//自定义量 matchparent区别在于 size不一样
            mwidth = widthSpecSize;
        } else {
            mwidth = 300;
        }
        if (widthSpecSize == MeasureSpec.UNSPECIFIED) {
            Log.d("MyView", "---UNSPECIFIED---");
        }
        mwidth = widthSpecSize;
        mheight = heightSpecSize;
        setMeasuredDimension(mwidth, mheight);
    }
}
